/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.solr.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @author huhailiang937
 * @date 2017年5月6日下午1:42:47
 * @since 1.0.0
 */
public class SqlFormatSupport {

    private static Pattern SQL_PATTERN = Pattern.compile("\\s{2,}|\t|\r|\n");

    /**
     * 格式化SQL，替换“换行”、“制表符”为空格
     * 
     * @param sql
     * @return
     */
    public static String formatBlankSpace(String sql) {
        String destSql = "";
        if (null != sql && !sql.isEmpty()) {
            Matcher m = SQL_PATTERN.matcher(sql);
            destSql = m.replaceAll(" ");
        }
        return destSql;
    }

    public static boolean isSlorNativeSql(String sql) {
        if (null != sql && !sql.isEmpty()) {
            return StringUtils.containsIgnoreCase(sql, "SolrNative");
        }
        return false;
    }

    // TODO sqlpaser
    public static boolean containsLimit(String sql) {
        String[] tokens = sql.split("\\s+");
        String secondToLastToken = tokens[tokens.length - 2];
        return ("limit").equalsIgnoreCase(secondToLastToken);
    }
}
